package com.sportradar.assessment.work.threads;

import java.util.Calendar;
import java.util.Date;
import java.util.Enumeration;
import java.util.Random;
import java.util.ResourceBundle;
import java.util.concurrent.Callable;

import com.sportradar.assessment.work.beans.ConfigurationBean;
import com.sportradar.assessment.work.services.ThreadManager;
import com.sportradar.assessment.work.util.ConfigUtil;
import com.sportradar.assessment.work.util.Constants;

public class ReadingConfigThread implements Runnable{

	private ThreadManager threadManager;

	public ReadingConfigThread(ThreadManager threadManager){
		this.threadManager=threadManager;
	}
	
	public void run()  {
		
		
			//System.out.println("ReadingConfigThread ");
			
			//1) last update
			System.out.println("Last Update was in "+threadManager.getLastUpdate());
			
			//2) Current Update
			Calendar nowAsCalendar = Calendar.getInstance();	
			System.out.println("Update now: "+nowAsCalendar.getTime());
			threadManager.setLastUpdate(nowAsCalendar.getTime());
					
			//3) Next Update
			long refreshRateInSeconds=ConfigUtil.getPropertyValueAsLong(Constants.REFRESH_INTERVAL_KEY);
			Calendar nextUpdate =  Calendar.getInstance();	
			nextUpdate.setTime(threadManager.getLastUpdate());
			nextUpdate.add(Calendar.SECOND, (int)refreshRateInSeconds);
			System.out.println("next Update will be "+nextUpdate.getTime());
			
			ConfigurationBean updatedConfiguration=ConfigUtil.update();
			this.threadManager.updateSettings(updatedConfiguration);
		
	}
	
	

}
